home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / menuscripter / sources / msaemenuutils.c < prev    next >
Encoding:
Text File  |  2000-06-23  |  5.1 KB  |  237 lines

  1. // MSAEMenuUtils.c
  2. //
  3. // Original version by Jon Lansdell and Nigel Humphreys.
  4. // 4.0 and 3.1 updates by Greg Sutton.
  5. // ©Apple Computer Inc 1996, all rights reserved.
  6.  
  7. #include "MSAEMenuUtils.h"
  8.  
  9. #include <TextUtils.h>
  10. #ifdef THINK_C
  11.     #include "PLStrs.h"
  12. #else
  13.     #include <PLStringFuncs.h>
  14. #endif
  15. #include <AEPackObject.h>
  16.  
  17. #include "MSGlobals.h"
  18. #include "MSAEWindowUtils.h"
  19.  
  20.  
  21. OSErr    MenuNameToMenuToken( StringPtr theName, MenuToken *theToken )
  22. {
  23.     short   index,
  24.             numMenus = CountMenus( );
  25.     
  26.     for ( index = appleM; index <= numMenus ; index++ )
  27.     {
  28.         if ( IdenticalString( theName, (**(myMenus[index])).menuData, NULL ) == 0 )
  29.         {
  30.             theToken->tokenMenu = myMenus[index];
  31.             theToken->tokenID = index + appleID;
  32.             return noErr;
  33.         }
  34.     }
  35.  
  36.     return errAENoSuchObject;
  37. }
  38.  
  39.     
  40. OSErr    GetDescOfNamedMenu( StringPtr theName, AEDesc* result )
  41. {
  42.     MenuToken        theToken;
  43.     OSErr            err = noErr;
  44.     
  45.     err = MenuNameToMenuToken( theName, &theToken );
  46.     if ( noErr != err ) goto done;
  47.     
  48.     err = AECreateDesc( typeMyMenu, (Ptr)&theToken, sizeof( theToken ), result );
  49.  
  50. done:
  51.     return err;
  52. }
  53.  
  54.  
  55. OSErr    GetDescOfNthMenu( short theIndex, AEDesc* result )
  56. {
  57.     MenuToken        theToken;
  58.     OSErr            err = noErr;
  59.     
  60.     if ( theIndex <= CountMenus( ) )
  61.     {
  62.         theToken.tokenMenu = myMenus[theIndex];
  63.         theToken.tokenID = theIndex + appleID;
  64.         err = AECreateDesc( typeMyMenu, (Ptr)&theToken, sizeof( theToken ), result );
  65.     }
  66.     else
  67.         err = errAEIllegalIndex;
  68.  
  69.     return err;
  70. }
  71.  
  72.  
  73. short    CountMenus( void )
  74. {
  75.     if ( CountDocuments( ) > 0 )
  76.         return kLastMenu + 1;
  77.     else
  78.         return editM + 1;
  79. }
  80.  
  81.  
  82. void    GetMenuName( MenuToken* theToken, StringPtr theResult )
  83. {
  84.     if ( theResult )
  85.         PLstrcpy( theResult, (**theToken->tokenMenu).menuData );    // Destination first
  86. }
  87.  
  88.  
  89. OSErr    MenuItemNameToMenuItemToken( MenuToken* containerToken, StringPtr theName, MenuItemToken *theToken )
  90. {
  91.     short   index,
  92.             numItems = CountMenuTokenItems( containerToken );
  93.     Str255    pStr;
  94.     
  95.     for ( index = 1; index <= numItems ; index++ )
  96.     {
  97.         GetMenuItemText( containerToken->tokenMenu, index, pStr );
  98.     
  99.         if ( IdenticalString( theName, pStr, NULL ) == 0 )
  100.         {
  101.             theToken->tokenMenuToken = *containerToken;
  102.             theToken->tokenItem = index;
  103.             return noErr;
  104.         }
  105.     }
  106.  
  107.     return errAENoSuchObject;
  108. }
  109.  
  110.     
  111. OSErr    GetDescOfNamedMenuItem( MenuToken* containerToken, StringPtr theName, AEDesc* result )
  112. {
  113.     MenuItemToken    theToken;
  114.     OSErr            err;
  115.     
  116.     err = MenuItemNameToMenuItemToken( containerToken, theName, &theToken );
  117.     if ( noErr != err ) goto done;
  118.     
  119.     err = AECreateDesc( typeMyMenuItem, (Ptr)&theToken, sizeof( theToken ), result );
  120.  
  121. done:
  122.     return err;
  123. }
  124.  
  125.  
  126. OSErr    GetDescOfNthMenuItem( MenuToken* containerToken, short theIndex, AEDesc* result )
  127. {
  128.     MenuItemToken    aToken;
  129.     OSErr            err = noErr;
  130.     
  131.     if ( theIndex <= CountMenuTokenItems( containerToken ) )
  132.     {
  133.         aToken.tokenMenuToken = *containerToken;
  134.         aToken.tokenItem = theIndex;
  135.         err = AECreateDesc( typeMyMenuItem, (Ptr)&aToken, sizeof( aToken ), result );
  136.     }
  137.     else
  138.         err = errAEIllegalIndex;
  139.  
  140.     return err;
  141. }
  142.  
  143.  
  144. short    CountMenuTokenItems( MenuToken* containerToken )
  145. {
  146.     return CountMItems( containerToken->tokenMenu );
  147. }
  148.  
  149.  
  150. void    GetMenuItemName( MenuItemToken* theToken, StringPtr theResult )
  151. {
  152.     GetMenuItemText( theToken->tokenMenuToken.tokenMenu,
  153.                                         theToken->tokenItem, theResult );
  154. }
  155.  
  156. void    SetMenuItemName( MenuItemToken* theToken, StringPtr theResult )
  157. {
  158.     SetMenuItemText( theToken->tokenMenuToken.tokenMenu,
  159.                                         theToken->tokenItem, theResult );
  160. }
  161.  
  162.  
  163. OSErr    MakeMenuSpecifier( MenuToken* theToken, AEDesc* theResult )
  164. {
  165.     AEDesc        nullSpec = {typeNull, NULL},
  166.                 aDesc = {typeNull, NULL};
  167.     Str255        aPStr;
  168.     OSErr        anErr;
  169.     
  170.     GetMenuName( theToken, aPStr );
  171.     
  172.     anErr = AECreateDesc( typeChar, (Ptr)&aPStr[1], aPStr[0], &aDesc );
  173.     if ( noErr != anErr ) goto done;
  174.     
  175.     anErr = CreateObjSpecifier( cMenu, &nullSpec, formName,
  176.                                             &aDesc, false, theResult );
  177.     
  178. done:
  179.     (void)AEDisposeDesc( &aDesc );
  180.  
  181.     return anErr;
  182. }
  183.  
  184.  
  185. OSErr    MakeMenuItemSpecifier( MenuItemToken* theToken, AEDesc* theResult )
  186. {
  187.     AEDesc        menuSpec = {typeNull, NULL},
  188.                 aDesc = {typeNull, NULL};
  189.     Str255        aPStr;
  190.     OSErr        anErr;
  191.     
  192.     anErr = MakeMenuSpecifier( &theToken->tokenMenuToken, &menuSpec );
  193.     if ( noErr != anErr ) goto done;
  194.     
  195.     GetMenuItemName( theToken, aPStr );
  196.     
  197.     anErr = AECreateDesc( typeChar, (Ptr)&aPStr[1], aPStr[0], &aDesc );
  198.     if ( noErr != anErr ) goto done;
  199.     
  200.     anErr = CreateObjSpecifier( cMenuItem, &menuSpec, formName,
  201.                                                     &aDesc, false, theResult );
  202.  
  203. done:
  204.     (void)AEDisposeDesc( &aDesc );
  205.     (void)AEDisposeDesc( &menuSpec );
  206.     
  207.     return anErr;
  208. }
  209.  
  210. MenuHandle    MenuHandleFromMenuID( short theMenuID )
  211. {
  212.     long        anIndex;
  213.     MenuHandle    aResult = NULL;
  214.  
  215.     anIndex = theMenuID - appleID;
  216.     if ( anIndex >= 0 && anIndex <= kLastMenu )
  217.         aResult = myMenus[anIndex];
  218.     
  219.     return aResult;    
  220. }
  221.  
  222. // Scripts are stored in a resource with the ID determined by the  menu and item
  223. //  (see top of MSScript.c) this routine converts this resource ID back to a token.
  224.  
  225. void    MenuTokenFromResID( short theResID, MenuToken* theToken )
  226. {
  227.     theToken->tokenID = theResID / 32;
  228.     theToken->tokenMenu = MenuHandleFromMenuID( theToken->tokenID );
  229. }
  230.  
  231. void    MenuItemTokenFromResID( short theResID, MenuItemToken* theToken )
  232. {
  233.     MenuTokenFromResID( theResID, &theToken->tokenMenuToken );
  234.     theToken->tokenItem = theResID % 32;
  235. }
  236.  
  237.